﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.IO.IsolatedStorage;
using System.Xml.Linq;
using System.Collections.ObjectModel;
using Microsoft.Phone;
using System.Windows.Media.Imaging;
using System.IO;
using Microsoft.Phone.Shell;
using ActivityTracker.Library;
using Common.Library;

namespace ActivityTracker
{
    public partial class MyActivities : PhoneApplicationPage
    {
        #region Global

        Actvity objActivity = new Actvity();
        ActivityData objActivityData = new ActivityData();
        Schedules objSchedules = new Schedules();
        int count = 20;

        #endregion

        #region contstructor

        public MyActivities()
        {
            InitializeComponent();
        }

        #endregion

        #region Page events

        private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            try
            {
                lbxActivities.ItemsSource = objActivity.LoadActivities(count);
            }
            catch (Exception ex)
            {
                string mess = "Exception in PhoneApplicationPage_Loaded Method In MyActivities file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void lbxActivities_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                if (lbxActivities.SelectedIndex == -1)
                    return;
                string name = "";
                name = (lbxActivities.SelectedItem as ActivityProp).ActName;
                if (name == "get more")
                {
                    count = count + 20;
                    lbxActivities.ItemsSource = objActivity.LoadActivities(count);
                    lbxActivities.ScrollIntoView(lbxActivities.Items[count - 20]);
                }
            }
            catch (Exception ex)
            {
                string mess = "Exception in lbxActivities_SelectionChanged Method In MyActivities file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppnBarNew_Click(object sender, EventArgs e)
        {
            try
            {
                objActivityData.ActivityOldDataClearing();
                NavigationService.Navigate(new Uri("/Views/Activities/AddNewActivity.xaml", UriKind.Relative));
            }
            catch (Exception ex)
            {
                string mess = "Exception in AppnBarNew_Click Method In MyActivities file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppBarEdit_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbxActivities.SelectedIndex != -1)
                {
                    string actId = (lbxActivities.SelectedItem as ActivityProp).ActId;
                    if (App.runningActId != actId)
                    {
                        IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
                        if (isoStore.FileExists("ActivityTracker/TempCategories.xml"))
                            isoStore.DeleteFile("ActivityTracker/TempCategories.xml");
                        NavigationService.Navigate(new Uri("/Views/Activities/AddNewActivity.xaml?actId=" + actId, UriKind.Relative));
                    }
                    else
                        MessageBox.Show("This activity is running. You cannot edit this activity");
                }
                else
                    MessageBox.Show("Please select an activity");
            }
            catch (Exception ex)
            {
                string mess = "Exception in AppBarEdit_Click Method In MyActivities file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppBarDelete_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbxActivities.SelectedIndex != -1)
                {
                    int selIndex = lbxActivities.SelectedIndex;
                    string actId = (lbxActivities.SelectedItem as ActivityProp).ActId;
                    if (App.runningActId != actId.ToString())
                    {
                        string DeleteConfirmation = objActivity.DeleteActivityConfirmation(actId);
                        MessageBoxResult res = MessageBox.Show("Are you sure you want to delete this activity?", DeleteConfirmation, MessageBoxButton.OKCancel);
                        if (res == MessageBoxResult.OK)
                        {
                            objSchedules.DeleteSchedulesBasedOnActId(actId);
                            objActivity.DeleteActivity(actId);
                            lbxActivities.ItemsSource = objActivity.LoadActivities(count);
                            lbxActivities.SelectedIndex = selIndex - 1;
                        }
                    }
                    else
                        MessageBox.Show("This activity is running. You cannot delete this activity");
                }
                else
                    MessageBox.Show("Please select an activity");

            }
            catch (Exception ex)
            {
                string mess = "Exception in AppBarDelete_Click Method In MyActivities file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppBarNewSchedule_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbxActivities.SelectedIndex != -1)
                {
                    string actId = (lbxActivities.SelectedItem as ActivityProp).ActId;
                    NavigationService.Navigate(new Uri("/Views/Schedules/AddNewSchedule.xaml?actId=" + actId, UriKind.Relative));
                }
                else
                    MessageBox.Show("Please select an activity");
            }
            catch (Exception ex)
            {
                string mess = "Exception in AppBarNewSchedule_Click Method In MyActivities file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        private void AppBarAddPastActivity_Click(object sender, EventArgs e)
        {
            try
            {
                if (lbxActivities.SelectedIndex != -1)
                {
                    string actId = (lbxActivities.SelectedItem as ActivityProp).ActId;
                    NavigationService.Navigate(new Uri("/Views/Activities/AddPastActivityHistory.xaml?actId=" + actId, UriKind.Relative));
                }
                else
                    MessageBox.Show("Please select an activity");
            }
            catch (Exception ex)
            {
                string mess = "Exception in AppBarAddPastActivity_Click Method In MyActivities file.\n\n" + ex.Message + "\n\n Stack Trace:- " + ex.StackTrace;
                Exceptions.SaveOrSendExceptions(mess, ex);
            }
        }

        #endregion
    }
}